package com.jungle.design.mode.strategypattern.entity.enums;

import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author Jungle
 * @create 2024-09-15 22:16
 */
@Getter
@AllArgsConstructor
public enum PayEnum {
    ALI_PAY(1),
    WECHAT_PAY(2),
    UNION_PAY(3);
    private final Integer payType;
    /**
     * 代码优化
     */
    private static final Map<Integer, PayEnum> PAY_TYPE_MAP =
            Stream.of(PayEnum.values()).collect(Collectors.toMap(PayEnum::getPayType, e -> e));


    public static PayEnum fromPayType(int payType) {
        PayEnum payEnum = PAY_TYPE_MAP.get(payType);
        if (payEnum == null) {
            throw new RuntimeException("Invalid pay type: " + payType);
        }
        return payEnum;
    }
}
